package com.lhm.controller;


import com.lhm.entity.Book;
import com.lhm.entity.Type;
import com.lhm.entity.User;
import com.lhm.service.BookService;
import com.lhm.service.MatterService;
import com.lhm.service.TypeService;
import com.lhm.service.UserService;
import com.lhm.tool.RespJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;



import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/fore")
public class UserController {

@Autowired
    TypeService typeService;
@Autowired
    BookService bookService;

@Autowired
    UserService userService;
@Autowired
    MatterService matterService;

    @RequestMapping("/hello")
    public String hello(){

        return "login";
    }

    @RequestMapping("/hi")
    public String hi(){
        return "index";
    }



    @RequestMapping("/helloword")
    public String hello1(){
        return "index";
    }


    @RequestMapping("/zhuce")
    public String zhuce(){
        return "public/my";
    }

    @RequestMapping("/geren")
    public String geren(){
        return "public/ge";
    }


    @ResponseBody
    @RequestMapping("/findType")//查询类型
    public Map findType(Model model){
        return RespJson.pack(200,"类型",typeService.findAll());
    }

    @ResponseBody
    @RequestMapping("/findByType/{id}")////类型查询
    public Map findByType(@PathVariable("id")String id){
        List<Book> bokes = bookService.findByTypeid(id);
        return RespJson.pack(200,"博客",bokes);
    }


    @RequestMapping("go2Book")
    public String go2Boke(){
        return "book/bookList";
    }

    @ResponseBody
    @RequestMapping("findById/{id}")//根据id查询
    public Map findById(@PathVariable("id")String id){
        return RespJson.pack(200,"成功",bookService.findById(id));
    }

    @ResponseBody
    @RequestMapping("findBy/{title}")//模糊查询
    public Map findBy(@PathVariable("title")String title){
        return RespJson.pack(200,"成功",bookService.findBy(title));
    }


    @ResponseBody
    @RequestMapping("/addUser")//注册
    public Map<String, Object> addUser(String password, String nickname,String username) {
        User u = new User();
        u.setRole("G");
        u.setPassword(password);
        u.setNickname(nickname);
        u.setUsername(username);
        userService.add(u);
        return RespJson.pack(200, "添加成功 ...","");
    }
    //删除
    @ResponseBody
    @RequestMapping({"/del/{id}"})
    public Map<String, Object> delete(@PathVariable String id) {

               bookService.delById(id);
        return RespJson.pack(200, "删除成功...", "");
    }

    //添加属性/修改属性

    @ResponseBody
    @RequestMapping({"/addType"})
    public Map addType(Type type){

        Map<String, Object> map  = typeService.addType(type);

        return map;
    }

}
